package com.mastercard.assets.web.struts.forms;

import com.mastercard.assets.jpa.TblUsuario;
import com.mastercard.assets.util.dao.MasterCardDAO;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginMailForm extends ActionForm {

    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        
        ActionErrors errors = new ActionErrors( ) ;
        
        // verifica que no este vacio el campo
		if( email == null || email.trim().length() == 0 ) {
                    // agrega el error
			errors.add( "EmailEmpty" , new ActionMessage( "com.mastercard.assets.web.errors.modules.loginMail.emailEmpty" ) ) ;
                }
                
                if( errors.size() > 0 )
                    return errors ;
                
                // busca el registro que corresponde al email
                List < TblUsuario > res = MasterCardDAO.findUserByEmail(email);                
                
                if ( res.isEmpty() ){
                    // en caso de estar vacio
                    // agrega el error
			errors.add( "NoEmail" , new ActionMessage( "com.mastercard.assets.web.errors.modules.loginMail.noEmail" ) ) ;
                }
                 
        MasterCardDAO.closeEntityManager( ) ;
        return errors ;        
    }
}
